package managedbean;

import java.io.Serializable;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class IndexManagedBean implements Serializable
{
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private String selectedLocale;
    
    public IndexManagedBean()
    {        
        // check if session has selectedLocale key alrd..
        // saved key exists then get from session and set to combo box
        if (ctx.getExternalContext().getSessionMap().containsKey("savedLocale")) {
            selectedLocale = ctx.getExternalContext().getSessionMap().get("savedLocale").toString();
//            System.out.println("Select 1: " + selectedLocale);
            if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
            else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
            else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
            else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        }
        // key exists then get from session and set to combo box
        if (ctx.getExternalContext().getSessionMap().containsKey("selectedLocale")) {
            selectedLocale = ctx.getExternalContext().getSessionMap().get("selectedLocale").toString();
//            System.out.println("Select 2: " + selectedLocale);
            if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
            else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
            else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
            else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        }
        // key does not exist then get from session and set to combo box
        else {
            ctx.getViewRoot().setLocale(new Locale("en", "US"));
            ctx.getExternalContext().getSessionMap().put("selectedLocale", ctx.getViewRoot().getLocale());
//            System.out.println("Select 3: " + ctx.getViewRoot().getLocale());
        }   
    }
    
    public void selectedLocaleValueChangeListener()
    {
        if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
        else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
        else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
        else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        
        ctx.getExternalContext().getSessionMap().put("selectedLocale", ctx.getViewRoot().getLocale());
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public String getSelectedLocale() {
        return selectedLocale;
    }

    public void setSelectedLocale(String selectedLocale) {
        this.selectedLocale = selectedLocale;
    }
   
}